<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class WalletModel extends Model
{
    //
    protected $table = 'wallets';

    protected $guarded = [];

    protected $dateFormat = 'U';

    // 20230803 by LC, 更新最新的wallet
    public static function updateUserScore( $userId, $scores, $userType ): bool
    {
        $data = static::query()->where(['user_id'=>$userId, 'user_type'=> $userType])->first();
        $t = time();
        // 不存在，添加
        if( empty($data) ){

            $data = [
                'user_id'       =>$userId,
                'user_type'     =>$userType,
                'created_at'    =>$t,
                'updated_at'    =>$t
            ];

            static::create( array_merge($data, $scores) );
        }else{
            $where = [
                'user_id'       =>$userId,
                'user_type'     =>$userType
            ];
            static::query()->where($where)->update(array_merge(['updated_at'=>$t], $scores));
        }
        return true;
    }
}
